package com.obtk.controllerSW;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.model.CstService;
import com.obtk.model.SelChance;
import com.obtk.service.CstLinkmanService;
import com.obtk.service.CstServiceService;
import com.obtk.service.SelChanceService;
import com.obtk.util.CoreConst;
import com.obtk.util.PageUtil;
import com.obtk.util.ResultUtil;
import com.obtk.vo.base.PageResultVo;
import com.obtk.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 功能说明
 *
 * @author Mr.Sun
 * @date 2023-04-05 15:48:15
 */
@Controller
@RequestMapping("/selChance")
public class SelChanceController {
    @Autowired
    private SelChanceService selChanceService;
    @Autowired
    private CstLinkmanService cstLinkmanService;
    //    @Autowired
//    private CustomerService customerService;
    @PostMapping("/list")
    @ResponseBody
    public PageResultVo loadUsers(SelChance selChance, Integer limit, Integer offset, Model model){
        PageHelper.startPage(PageUtil.getPageNo(limit, offset),limit);
        List<SelChance> selChanceList = selChanceService.selectSelChance(selChance);
        PageInfo<SelChance> pages = new PageInfo<>(selChanceList);
        System.out.println(selChanceList);

        return ResultUtil.table(selChanceList,pages.getTotal());
    }
    /**新增营销机会*/
    @PostMapping("/add")
    @ResponseBody
    public ResponseVo add(SelChance selChanceForm){
        String chcCustName = selChanceForm.getChcCustName();
        SelChance selChance = selChanceService.selectByChcCustName(chcCustName);
        if (null != selChance) {
            return ResultUtil.error("联系人已存在");
        }

        int num = selChanceService.register(selChanceForm);
        if(num > 0){
            return ResultUtil.success("添加联系人成功");
        }else {
            return ResultUtil.error("添加联系人失败");
        }
    }


    /**编辑用户详情*/
    @GetMapping("/edit")
    public String userDetail(Model model, Integer id){
        SelChance selChance = selChanceService.selectBySelChanceId(id);
        System.out.println("controller"+id);
        model.addAttribute("selChance", selChance);
        return "selChance/update";
    }

    /**编辑用户*/
    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo editUser(SelChance selChanceForm){

        int a = selChanceService.updateBySelChanceId(selChanceForm);
        if (a > 0) {
            return ResultUtil.success("编辑成功！");
        } else {
            return ResultUtil.error("编辑失败");
        }
    }

    /**用户查看详情*/
    @GetMapping("/edit2")
    public String userDetail2(Model model, Integer id){
        SelChance selChance = selChanceService.selectBySelChanceId(id);
        System.out.println("controller"+id);
        model.addAttribute("selChance", selChance);
        return "selChance/update2";
    }
    /**删除机会*/
    @GetMapping("/delete")
    @ResponseBody
    public ResponseVo deleteUser(String id) {
        List<String> cstServiceIdsList = Arrays.asList(id);
        System.out.println("是什么？"+id);
        int a = selChanceService.updateStatusBatch(cstServiceIdsList, CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除成功");
        } else {
            return ResultUtil.error("删除失败");
        }
    }


    /** 更改为客户 */
    /**删除营销机会 并且删除联系人*/
    @GetMapping("/delete2")
    @ResponseBody
    public ResponseVo deleteUser2(String id,String lkmName) {
        List<String> cstServiceIdsList = Arrays.asList(id);
        int a = selChanceService.updateStatusBatch(cstServiceIdsList, CoreConst.STATUS_INVALID);
        System.out.println(cstServiceIdsList);
        System.out.println("lkmId是什么:"+lkmName);
        String s = String.valueOf(lkmName);
        int b = cstLinkmanService.updateLkmName(s,CoreConst.STATUS_INVALID);
        if (a == b || a >0) {
            return ResultUtil.success("联系人更改为客户成功!");
        } else {
            return ResultUtil.error("更改失败!");
        }
    }

    @GetMapping("/batch/delete")
    @ResponseBody
    public ResponseVo batchDeleteUser(String idsStr) {
        String[] ids = idsStr.split(",");
        List<String> cstServiceIdsList = Arrays.asList(ids);
        int a = selChanceService.updateStatusBatch(cstServiceIdsList,CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("更改成功");
        } else {
            return ResultUtil.error("更改失败");
        }
    }
}
